﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OneMile.DAL;
using OneMile.Entity;
using OneMile.Entity.Model;
using OneMile.Common;
namespace OneMile.BLL
{
   public  class BLLUFavorites:BaseBLL
    {
       private DALUFavorites dalFav;
       public DALUFavorites DALFav
       {
           get
           {
               if (dalFav == null)
                   dalFav = DALFactory.getInstance().CreateUFavoritesDAL();
               return dalFav;
           }
       }


       public IList<EShopsInfo> GetFavoritesListByUser(string userId, int pageNo, int pageSize,string shopsType, int SortField, int SortType)
       {
           try
           {
               return DALFav.GetFavoritesListByUser(userId,pageNo,pageSize,shopsType,SortField,SortType);
           }
           catch (Exception e)
           {
               
               base.WriteLog(e.Message);
               throw e;
           }
       }

       public IList<EShopsInfo> GetFavoritesListByUserLocation(string userId, int PageNo, int PageSize, double Latitude, double Longitude)
       {
           try
           {
               IList<EShopsInfo> list = DALFav.GetFavoritesListByUserLocation(userId);
               if (list == null || list.Count == 0)
               {
                   return null;
               }
               for (int i = 0; i < list.Count; i++)
               {
                   list[i].Distance = LocationUtil.GetDistance(Latitude, Longitude, list[i].Latitude, list[i].Longitude);
               }
               string PropertyName = Enum.Format(typeof(Enums.EnumShopSortField), 12, "g");//按照距离排序

               return list.OrderBy(p => p.GetType().GetProperty(PropertyName)).Skip(PageSize * (PageNo - 1)).Take(PageSize).ToList<EShopsInfo>();
           }
           catch (Exception e)
           {
               base.WriteLog(e.Message);
               throw e;
           }
       }

       public bool DelFavorite(string userCollectionId)
       {
           try
           {
               return DALFav.DelFavorite(userCollectionId);
           }
           catch (Exception e)
           {
               base.WriteLog(e.Message);
               throw e;
           }
       }

       public bool AddNewFavorite(EUserCollection fav)
       {
           try
           {
               return DALFav.AddNewFavorite(fav);
           }
           catch (Exception e)
           {
               
               base.WriteLog(e.Message);
               throw e;
           }
       }

       public bool IsFavoriteExit(string userId, string shopId)
       {
           try
           {
               return DALFav.IsFavoriteExit(userId, shopId);
           }
           catch (Exception e)
           {
               
               base.WriteLog(e.Message);
               throw e;
           }
       }
    }
}
